import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/http/bean/wroks/contract_paymenttermscopy.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/image_path_utils.dart';
import 'package:flutter_html/flutter_html.dart' as html;

///合同审核 付款方式
class UiPayment {
  ///ContractPaymentTerms 付款方式(合同审核里的)
  static Widget build(BuildContext context, ContractPaymentTerms? data) {
    List<Widget> children = [];
    children
        .add(UiBase.title(context, AppStrings.of(context).TermsPayment)); //标题
    int index = 0;
    int size = data?.Info_list?.length ?? 0;
    for (int i = 0; i < size; i++) {
      index = i;
      String? title = null;
      bool? isSelected = null;
      if (index == 5) {
        //分期付款
        title = AppStrings.of(context).PaymentInstallment + " ";
        isSelected = data?.isSelected(0);
      } else if (index == 6) {
        //分批付款
        title = AppStrings.of(context).PartialPayment + " ";
        isSelected = data?.isSelected(1);
      } else if (index == 7) {
        //月结付款
        title = AppStrings.of(context).MonthlyPayment + " ";
        isSelected = data?.isSelected(2);
      } else if (index == 8) {
        //fixme 款到发货
        title = AppStrings.of(context).YourShipment + " ";
        isSelected = data?.isSelected(3);
      } else if (index == 9) {
        //日结付款
        title = AppStrings.of(context).DailyPayment + " ";
        isSelected = data?.isSelected(4);
      } else if (index == 10) {
        //fixme 款到生产
        title = AppStrings.of(context).ModelProduce + " ";
        isSelected = data?.isSelected(5);
      }
      children.add(_item(context, data?.Info_list?[index],
          title: title, isSelected: isSelected));
    }
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  ///付款方式（运输合同审核详情）
  static Widget build2(BuildContext context, ContractPaymentTerms? data,
      {ContractPaymentTerms? oldData = null}) {
    List<Widget> children = [];
    children
        .add(UiBase.title(context, AppStrings.of(context).TermsPayment)); //标题
    int index = 0;
    int size = data?.Info_list?.length ?? 0;
    for (int i = 0; i < size; i++) {
      index = i;
      String? title = null;
      bool? isSelected = null;
      if (index == 5) {
        //分期付款
        title = AppStrings.of(context).PaymentInstallment + " ";
        isSelected = data?.isSelected(0);
      } else if (index == 6) {
        //分批付款
        title = AppStrings.of(context).PartialPayment + " ";
        isSelected = data?.isSelected(1);
      } else if (index == 7) {
        //月结付款
        title = AppStrings.of(context).MonthlyPayment + " ";
        isSelected = data?.isSelected(2);
      } else if (index == 8) {
        //fixme 款到收货
        title = AppStrings.of(context).YourShipment2 + " ";
        isSelected = data?.isSelected(3);
      } else if (index == 9) {
        //日结付款
        title = AppStrings.of(context).DailyPayment + " ";
        isSelected = data?.isSelected(4);
      } else if (index == 10) {
        //fixme 款到生产；采购合同审核里应该没有。
        title = AppStrings.of(context).ModelProduce + " ";
        isSelected = data?.isSelected(5);
      }
      bool isColorRed = false;
      var content = data?.Info_list?[index];
      if (oldData != null &&
          oldData?.Info_list != null &&
          oldData!.Info_list!.length > index) {
        var oldContent = oldData?.Info_list?[index];
        var b = UiBase.isNotSame(content, oldContent);
        if (b) {
          isColorRed = true; //fixme 不相同，红色标注。
        }
      }
      children.add(_item(context, content,
          title: title, isSelected: isSelected, isColorRed: isColorRed));
    }
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  ///付款方式（采购合同审核详情）
  static Widget build3(BuildContext context, ContractPaymentTerms? data,
      {ContractPaymentTerms? oldData = null}) {
    List<Widget> children = [];
    children
        .add(UiBase.title(context, AppStrings.of(context).TermsPayment)); //标题
    int index = 0;
    int size = data?.Info_list?.length ?? 0;
    TextStyle textStyle = AppStyle.textStyleWithCard();
    TextStyle textStyleRed = AppStyle.textStyleWithCardRed();
    // print("$data");
    // print("旧数据：$oldData");
    // print("${data?.Info_list}");
    // [0.00|0.00|首批冲抵x%，末批冲抵x%|0.00|0.00 , 0.00 |0.00|, 1|1|1|1|0.00, 1|1|1|1|0.00, 0, 0|100, 0|100, 1|100.00|1|28, 付款后发货。, 1|100]
    // print("${data?.Status_list}");
    // print("旧数据：${oldData?.Info_list}");
    // print("${oldData?.Status_list}");
    for (int i = 0; i < size; i++) {
      index = i;
      String? title = null;
      bool? isSelected = null;
      bool titleRed = false; //历史选中的变红（即上一次选中的）
      if (index == 5) {
        //分期付款
        title = AppStrings.of(context).PaymentInstallment + " ";
        isSelected = data?.isSelected(0);
        var oldIsSelected = oldData?.isSelected(0);
        // print("${data?.Status_list}");
        // print("${oldData?.Status_list}");
        // print("=======================$isSelected\t$oldIsSelected");
        if (oldData != null &&
            isSelected != oldIsSelected &&
            (isSelected != null && !isSelected)) {
          titleRed = true;
        }
      } else if (index == 6) {
        //分批付款
        title = AppStrings.of(context).PartialPayment + " ";
        isSelected = data?.isSelected(1);
        var oldIsSelected = oldData?.isSelected(1);
        // print("=======================$isSelected\t$oldIsSelected\t${isSelected.toString() != oldIsSelected.toString()}\ttitleRed:$titleRed");
        if (oldData != null &&
            isSelected != oldIsSelected &&
            (isSelected != null && !isSelected)) {
          //当前选中的不变红
          titleRed = true;
        }
      } else if (index == 7) {
        //月结付款
        title = AppStrings.of(context).MonthlyPayment + " ";
        isSelected = data?.isSelected(2);
        var oldIsSelected = oldData?.isSelected(2);
        if (oldData != null &&
            isSelected != oldIsSelected &&
            (isSelected != null && !isSelected)) {
          titleRed = true;
        }
      } else if (index == 8) {
        //fixme 款到收货
        title = AppStrings.of(context).YourShipment2 + " ";
        isSelected = data?.isSelected(3);
        var oldIsSelected = oldData?.isSelected(3);
        if (oldData != null &&
            isSelected != oldIsSelected &&
            (isSelected != null && !isSelected)) {
          titleRed = true;
        }
      } else if (index == 9) {
        //日结付款
        title = AppStrings.of(context).DailyPayment + " ";
        isSelected = data?.isSelected(4);
        var oldIsSelected = oldData?.isSelected(4);
        if (oldData != null &&
            isSelected != oldIsSelected &&
            (isSelected != null && !isSelected)) {
          titleRed = true;
        }
      } else if (index == 10) {
        //fixme 款到生产；采购合同审核里应该没有。
        title = AppStrings.of(context).ModelProduce + " ";
        isSelected = data?.isSelected(5);
        var oldIsSelected = oldData?.isSelected(5);
        if (oldData != null &&
            isSelected != oldIsSelected &&
            (isSelected != null && !isSelected)) {
          titleRed = true;
        }
      }
      var notChangeColor =
          !(isSelected ?? false); //fixme 是否不变色；没有选中的时候，就不变色。【和erp保持一致】
      var content = data?.Info_list?[index];
      var contentOld = oldData?.Info_list?[index];
      // print("$context");
      // print("$contentOld");
      if (contentOld == null) {
        contentOld = content;
      }
      Widget? contentChild = null;
      var strData = content?.split("|");
      var oldStrData = contentOld?.split("|");
      // print("下标$index");
      // print("数据：$strData");
      // print("数据：$oldStrData");
      try {
        if (index == 0) {
          // content =
          //     AppStrings.of(context).Paymentmethod0(strData![0], strData![1], strData![2]);
          var rchildren = <TextSpan>[];
          rchildren.add(TextSpan(
              text: " ${strData![0]} ",
              style: NumUtil.strToDoubleStr(strData![0]) ==
                      NumUtil.strToDoubleStr(oldStrData![0])
                  ? textStyle
                  : textStyleRed));
          rchildren.add(TextSpan(text: "%,付款", style: textStyle));
          rchildren.add(TextSpan(
              text: " ${strData![1]} ",
              style: NumUtil.strToDoubleStr(strData![1]) ==
                      NumUtil.strToDoubleStr(oldStrData![1])
                  ? textStyle
                  : textStyleRed));
          rchildren.add(TextSpan(text: "元。冲抵方式", style: textStyle));
          rchildren.add(TextSpan(
              text: " ${strData![2]} ",
              style: strData![2] == oldStrData![2] ? textStyle : textStyleRed));
          // [0.00|0.00|首批冲抵x%，末批冲抵x%|0.00|0.00 , 0.00 |0.00|, 1|1|1|1|0.00, 1|1|1|1|0.00, 0, 0|100, 0|100, 1|100.00|1|28, 付款后发货。, 1|100]
          // print("strData:$strData\t${strData.length}");
          if ((strData.toString().contains(" 首批冲抵") ||
                  strData.toString().contains("末批冲抵")) &&
              strData.length >= 5 &&
              oldStrData.length >= 5) {
            rchildren.add(TextSpan(text: "首批冲抵比例", style: textStyle));
            rchildren.add(TextSpan(
                text: " ${strData![3]} %",
                style:
                    strData![3] == oldStrData![3] ? textStyle : textStyleRed));
            rchildren.add(TextSpan(text: " 末批冲抵比例", style: textStyle));
            rchildren.add(TextSpan(
                text: " ${strData![4]} %",
                style:
                    strData![4] == oldStrData![4] ? textStyle : textStyleRed));
          }
          // rchildren.add(value);
          // rchildren.add(value);
          // rchildren.add(value);
          // rchildren.add(value);
          contentChild = Text.rich(
              TextSpan(text: "1、预付", style: textStyle, children: rchildren));
        } else if (index == 1) {
          contentChild =
              Text.rich(TextSpan(text: "2、铺底 ", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: NumUtil.strToDoubleStr(strData![0]) ==
                        NumUtil.strToDoubleStr(oldStrData![0])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "元", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: NumUtil.strToDoubleStr(strData![1]) ==
                        NumUtil.strToDoubleStr(oldStrData![1])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "方。支付方式", style: textStyle),
            TextSpan(
                text: " ${strData![2]} ",
                style:
                    strData![2] == oldStrData![2] ? textStyle : textStyleRed),
          ]));
        } else if (index == 2) {
          contentChild =
              Text.rich(TextSpan(text: "3、年中", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: NumUtil.strToDoubleStr(strData![0]) ==
                        NumUtil.strToDoubleStr(oldStrData![0])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "月", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: NumUtil.strToDoubleStr(strData![1]) ==
                        NumUtil.strToDoubleStr(oldStrData![1])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "日前，需付", style: textStyle),
            TextSpan(
                text: " ${strData![2]} ",
                style: NumUtil.strToDoubleStr(strData![2]) ==
                        NumUtil.strToDoubleStr(oldStrData![2])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "月", style: textStyle),
            TextSpan(
                text: " ${strData![3]} ",
                style: NumUtil.strToDoubleStr(strData![3]) ==
                        NumUtil.strToDoubleStr(oldStrData![3])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "日前发货的", style: textStyle),
            TextSpan(
                text: " ${strData![4]} ",
                style: NumUtil.strToDoubleStr(strData![4]) ==
                        NumUtil.strToDoubleStr(oldStrData![4])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "%货款。", style: textStyle),
          ]));
        } else if (index == 3) {
          contentChild =
              Text.rich(TextSpan(text: "4、年末", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: NumUtil.strToDoubleStr(strData![0]) ==
                        NumUtil.strToDoubleStr(oldStrData![0])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "月", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: NumUtil.strToDoubleStr(strData![1]) ==
                        NumUtil.strToDoubleStr(oldStrData![1])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "日前，需付", style: textStyle),
            TextSpan(
                text: " ${strData![2]} ",
                style: NumUtil.strToDoubleStr(strData![2]) ==
                        NumUtil.strToDoubleStr(oldStrData![2])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "月", style: textStyle),
            TextSpan(
                text: " ${strData![3]} ",
                style: NumUtil.strToDoubleStr(strData![3]) ==
                        NumUtil.strToDoubleStr(oldStrData![3])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "日前发货的", style: textStyle),
            TextSpan(
                text: " ${strData![4]} ",
                style: NumUtil.strToDoubleStr(strData![4]) ==
                        NumUtil.strToDoubleStr(oldStrData![4])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "%货款。", style: textStyle),
          ]));
        } else if (index == 4) {
          contentChild =
              Text.rich(TextSpan(text: "5、末批发货", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: NumUtil.strToDoubleStr(strData![0]) ==
                        NumUtil.strToDoubleStr(oldStrData![0])
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "天后结清余款。", style: textStyle),
          ]));
        } else if (index == 5) {
          // print(
          //     "===========================================${strData![0]}\t${oldStrData![0]}\t${strData![0] == oldStrData![0]}");
          //fixme 分期付款 isSelected
          contentChild =
              Text.rich(TextSpan(text: "货款", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: ((NumUtil.strToDoubleStr(strData![0]) ==
                            NumUtil.strToDoubleStr(oldStrData![0])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "元时需付", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: ((NumUtil.strToDoubleStr(strData![1]) ==
                            NumUtil.strToDoubleStr(oldStrData![1])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "%货款后收货。", style: textStyle),
          ]));
        } else if (index == 6) {
          //fixme 分批付款
          contentChild =
              Text.rich(TextSpan(text: "收货", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: ((NumUtil.strToDoubleStr(strData![0]) ==
                            NumUtil.strToDoubleStr(oldStrData![0])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "方时需付", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: ((NumUtil.strToDoubleStr(strData![1]) ==
                            NumUtil.strToDoubleStr(oldStrData![1])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "%货款后收货。", style: textStyle),
          ]));
        } else if (index == 7) {
          //fixme 月结付款
          contentChild =
              Text.rich(TextSpan(text: "每月", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: ((NumUtil.strToDoubleStr(strData![0]) ==
                            NumUtil.strToDoubleStr(oldStrData![0])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "日前需付", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: ((NumUtil.strToDoubleStr(strData![1]) ==
                            NumUtil.strToDoubleStr(oldStrData![1])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "％前", style: textStyle),
            TextSpan(
                text: " ${strData![2]} ",
                style: ((NumUtil.strToDoubleStr(strData![2]) ==
                            NumUtil.strToDoubleStr(oldStrData![2])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "个月", style: textStyle),
            TextSpan(
                text: " ${strData![3]} ",
                style: ((NumUtil.strToDoubleStr(strData![3]) ==
                            NumUtil.strToDoubleStr(oldStrData![3])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "日前的货款后在收货。", style: textStyle),
          ]));
        } else if (index == 8) {
          //fixme 款到收货
          contentChild = Text.rich(TextSpan(
              text: strData![0],
              style: ((NumUtil.strToDoubleStr(strData![0]) ==
                          NumUtil.strToDoubleStr(oldStrData![0])) ||
                      notChangeColor)
                  ? textStyle
                  : textStyleRed));
        } else if (index == 9) {
          //fixme 日结付款
          contentChild =
              Text.rich(TextSpan(text: "收货", style: textStyle, children: [
            TextSpan(
                text: " ${strData![0]} ",
                style: ((NumUtil.strToDoubleStr(strData![0]) ==
                            NumUtil.strToDoubleStr(oldStrData![0])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "天时需付", style: textStyle),
            TextSpan(
                text: " ${strData![1]} ",
                style: ((NumUtil.strToDoubleStr(strData![1]) ==
                            NumUtil.strToDoubleStr(oldStrData![1])) ||
                        notChangeColor)
                    ? textStyle
                    : textStyleRed),
            TextSpan(text: "%货款。", style: textStyle),
          ]));
        }
      } catch (e) {
        contentChild = AppUi.getEmptyView();
      }
      // print(
      //     "=================================================title:$title\ttitleRed:$titleRed");
      children.add(_item3(context, contentChild!,
          title: title, isSelected: isSelected, titleRed: titleRed));
    }
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  ///ContractPaymentTerms 末批发货
  static Widget buildWithLast(
      BuildContext context, ContractPaymentTerms? data) {
    List<Widget> children = [];
    children.add(UiBase.title(context, AppStrings.of(context).EndGoods)); //标题
    int index = 0;
    int size = data?.Info_list?.length ?? 0;
    for (int i = 0; i < size; i++) {
      index = i;
      bool? isSelected = data?.isSelected(index);
      children.add(_item(context, data?.Info_list?[index],
          title: null, isSelected: isSelected));
    }
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  ///content内容
  ///isSelected 是否选中
  ///title 标题
  ///isColorRed 是否为红色字体
  static Widget _item(BuildContext context, String? content,
      {bool? isSelected, String? title, bool isColorRed = false}) {
    List<Widget> children = [];
    if (isSelected != null) {
      var path = ImagePathUtils.getPathWithImageLabel("ic_011_chacha.png");
      if (isSelected) {
        path = ImagePathUtils.getPathWithImageLabel("ic_011_gougou.png");
      }
      children.add(Image.asset(
        path,
        width: 14,
      ));
      children.add(SizedBox(
        width: 4,
      ));
    }
    TextStyle contentStyle = AppStyle.textStyleWithCard();
    if (isColorRed) {
      contentStyle = AppStyle.textStyleWithCardRed();
    }
    if (StringUtils.isNotEmpty(title)) {
      var titleV = Text(
        title ?? "",
        style: AppStyle.textStyleWithCardBold(),
      );
      children.add(titleV);
    }
    var text = Text(
      content ?? "",
      softWrap: true,
      style: contentStyle,
    );
    children.add(Expanded(child: text));
    return Padding(
      padding: EdgeInsets.only(bottom: 4),
      child: Row(
        children: children,
      ),
    );
  }

  static Widget _item3(BuildContext context, Widget content,
      {bool? isSelected, String? title, bool titleRed = false}) {
    List<Widget> children = [];
    if (isSelected != null) {
      var path = ImagePathUtils.getPathWithImageLabel("ic_011_chacha.png");
      if (isSelected) {
        path = ImagePathUtils.getPathWithImageLabel("ic_011_gougou.png");
      }
      children.add(Image.asset(
        path,
        width: 14,
      ));
      children.add(SizedBox(
        width: 4,
      ));
    }
    if (StringUtils.isNotEmpty(title)) {
      var titleTextStyle = AppStyle.textStyleWithCardBold();
      if (titleRed) {
        titleTextStyle = AppStyle.textStyleWithCardBoldRed();
      }
      var titleV = Text(
        title ?? "",
        style: titleTextStyle,
      );
      children.add(titleV);
    }
    children.add(Expanded(child: content));
    return Padding(
      padding: EdgeInsets.only(bottom: 4),
      child: Row(
        children: children,
      ),
    );
  }
}
